package com.example.medicalzb.controller;

import com.example.medicalzb.model.CityModel;
import com.example.medicalzb.service.CityService;
import com.example.medicalzb.util.Msg;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/api/citys")
@Api(tags = "城市信息控制类")
@CrossOrigin
public class CityController {
    @Autowired
    private CityService cityService;

    //查询全部城市
    @GetMapping({"/{pn}/{size}",""})
    public Msg getCityWithPage(@PathVariable(value = "pn", required = false) Integer pn,
                               @PathVariable(value = "size", required = false) Integer size,
                               @RequestParam(required = false) String name){
        PageInfo<CityModel> pageInfo = cityService.getCityWithPage(pn,size,name);
        if (pageInfo != null){
            return Msg.success().data("cityPageInfo",pageInfo);
        }
        return Msg.fail();
    }

    //根据id查找城市
    @GetMapping("{id}")
    public Msg getCityById(@PathVariable("id") Integer id){
        return cityService.getCityById(id);
    }

    //新增城市信息
    @PostMapping(value = "")
    @RolesAllowed({"1"})
    public Msg saveCity(Integer cityNumber){
        if (cityService.checkCityByNumber(cityNumber) > 0){
            return Msg.fail().mess("该城市已存在").code(100004);
        }
        return cityService.saveCity(cityNumber);
    }

    //删除城市信息
    @DeleteMapping("{id}")
    @RolesAllowed({"1"})
    public Msg deleteCityById(@PathVariable("id") Integer id){
        return cityService.deleteCityById(id);
    }
}
